#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "QDebug"
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    pic_num = 0;
    ui->setupUi(this);
    QList<QCameraInfo> cameras = QCameraInfo::availableCameras();
     foreach (const QCameraInfo &cameraInfo, cameras)
     {
         QCamera * m_camera=new QCamera(cameraInfo);
         camera_map[cameraInfo.description()]=m_camera;

         QCameraImageCapture *m_capture =new QCameraImageCapture(m_camera);
         capture_map[cameraInfo.description()]=m_capture;

         qDebug() << cameraInfo.deviceName()
                  <<cameraInfo.description();
     }
     QStringList str_camera_list;
     QMapIterator<QString, QCamera*> i(camera_map);
      while (i.hasNext()) {
          i.next();
          str_camera_list<<i.key();
          qDebug() << i.key() << ": " << i.value() << endl;
      }
      str_list_model=new QStringListModel(str_camera_list);
      ui->listView->setModel(str_list_model);




}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_listView_clicked(const QModelIndex &index)
{
    QString str_cam_info=str_list_model->data(index).toString();
    current_camera = camera_map.value(str_cam_info);
    current_capture=capture_map.value(str_cam_info);

    current_camera->setViewfinder(ui->widget_camera);
    current_camera->setCaptureMode(QCamera::CaptureStillImage);
    current_camera->start();
}

void MainWindow::on_pushButton_capture_clicked()
{
    QString filename = QString("%1%2.jpg")
            .arg("G:\\QT_project\\camera\\")
            .arg(pic_num++);
    current_capture->capture((filename));




    //on half pressed shutter button
    current_camera->searchAndLock();

    //on shutter button pressed
    current_capture->capture();

    //on shutter button released
   current_camera->unlock();
}
